package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Map<String, Object>> findAllMenu() {
        /*
        List<Map<String,Object>> menuData = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("id",1);
        map.put("label","系统设置");

        List<Map<String,Object>> children = new ArrayList<>();
        Map<String,Object> map2 = new HashMap<>();
        map2.put("id",2);
        map2.put("label","菜单管理");

        Map<String,Object> map3 = new HashMap<>();
        map3.put("id",3);
        map3.put("label","权限管理");

        children.add(map2);
        children.add(map3);

        map.put("children",children);
        menuData.add(map);
         */
        List<Map<String,Object>> menuData = new ArrayList<>();
        List<Menu> allMenu = menuDao.findAllMenu();
        for (Menu menu : allMenu) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",menu.getId());
            map.put("label",menu.getName());

            List<Menu> childrenMenu = menu.getChildren();
            List<Map<String,Object>> children = new ArrayList<>();
            for (Menu childrenMenu1 : childrenMenu) {
                Map<String,Object> map2 = new HashMap<>();
                map2.put("id",childrenMenu1.getId());
                map2.put("label",childrenMenu1.getName());
                children.add(map2);
            }
            map.put("children",children);
            menuData.add(map);
        }
        return menuData;
    }

    @Override
    public List<Integer> findAllMenuByRoleId(Integer roleId) {
        return menuDao.findAllMenuByRoleId(roleId);
    }

    @Override
    public PageResult findAllMenByRole(Integer id, QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询基于mybatis框架的PageHelper分页助手
        PageHelper.startPage(currentPage, pageSize);
        Page<Menu> page = menuDao.findAllMenByRole(id);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public PageResult queryPage(QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询基于mybatis框架的PageHelper分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page = menuDao.queryPage();
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Map<String, Object>> findByUsername(String username) {
        //根据用户名，查询角色id
        Integer Rid = roleDao.findRidByUsername(username);
        //根据角色id，获取可以访问的url
        List<Menu> parentUrl = menuDao.findParentUrl(Rid);
        //封装页面需要的数据
        List<Map<String, Object>> list = new ArrayList<>();
        for (Menu menu : parentUrl) {
            Map<String, Object> map = new HashMap<>();
            map.put("path", menu.getPath());
            map.put("title", menu.getName());
            map.put("icon", menu.getIcon());
            List<Menu> children = menu.getChildren();
            List<Map<String, Object>> childlist = new ArrayList<>();
            for (Menu child : children) {
                Map<String, Object> map2 = new HashMap<>();
                map2.put("path", child.getPath());
                map2.put("title", child.getName());
                map2.put("linkUrl", child.getLinkUrl());
                childlist.add(map2);
            }
            map.put("children", childlist);
            list.add(map);
        }
        return list;
    }


}
